﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Telerik.Web.Mvc.UI;
using TGVR_Project.Models;
using HomePage.Controllers;
using TGVR_Boot;
using TGVR_Boot.DTO;
using MDF.Worker;
using MDF.Logger.Managements;
using TGVR_Boot.Workers;

namespace TGVR_Project.Controllers
{
    public class TestChartController : BaseController
    {
        public ActionResult FirstLook()
        {
            ViewBag.Stack = false;
            ViewBag.SeriesType = "line";
            ViewBag.ShowTitle = true;
            
            ViewBag.ShowLegend = true;
            ViewBag.LegendPosition = ChartLegendPosition.Bottom;

            return View("~/Views/TestChart/FirstLook.cshtml");
        }
        public ActionResult _FirstLook(string fromcity, string tocity,
            DateTime DatePickerDepartDate,
            DateTime? DatePickerReturnDate)
        {
            return Json(GetCollection("Out", fromcity, tocity,
             DatePickerDepartDate,
             DatePickerReturnDate));
        }
        public ActionResult _FirstLook1(string fromcity, string tocity,
            DateTime DatePickerDepartDate,
            DateTime? DatePickerReturnDate)
        {
            return Json(GetCollection("In", fromcity, tocity,
             DatePickerDepartDate,
             DatePickerReturnDate));
        }
        private List<ComparePriceModel> GetCollection(string type, string fromcity, string tocity,
            DateTime DatePickerDepartDate,
            DateTime? DatePickerReturnDate)
        {
            // TODO: Initialize to an appropriate value
            string _strStationForm = fromcity; // TODO: Initialize to an appropriate value
            string _strStationTo = tocity; // TODO: Initialize to an appropriate value

            int iStart = fromcity.IndexOf('(');
            int iEnd = fromcity.IndexOf(')');

            _strStationForm = fromcity.Substring(iStart + 1, iEnd - iStart - 1);

            iStart = tocity.IndexOf('(');
            iEnd = tocity.IndexOf(')');

            _strStationTo = tocity.Substring(iStart + 1, iEnd - iStart - 1);

            DateTime _dtDateFrom = new DateTime(DatePickerDepartDate.Year, 
                DatePickerDepartDate.Month,
                DatePickerDepartDate.Day); // TODO: Initialize to an appropriate value

            Nullable<DateTime> _dtDateTo = null; // TODO: Initialize to an appropriate value

            if (DatePickerReturnDate != null)
            {
                _dtDateTo = new DateTime(DatePickerReturnDate.Value.Year, 
                    DatePickerReturnDate.Value.Month, 
                    DatePickerReturnDate.Value.Day);
            }
            int _iAdut = 1; // TODO: Initialize to an appropriate value
            int _iChild = 0; // TODO: Initialize to an appropriate value
            int _iBaby = 0; // TODO: Initialize to an appropriate value



            WorkerManager wkBus = new WorkerManager();

            wkBus.Add(new FindCheapestWorker("VietJetBoot", new VietJetBoot(), ComineTickets, _strStationForm, _strStationTo, _dtDateFrom, _dtDateTo, _iAdut, _iChild, _iBaby));

            wkBus.Add(new FindCheapestWorker("JetstarBoot", new JetstarBoot(), ComineTickets1, _strStationForm, _strStationTo, _dtDateFrom, _dtDateTo, _iAdut, _iChild, _iBaby));

            wkBus.Add(new FindCheapestWorker("VietNamAirline", new VietNamAirline(), ComineTickets2, _strStationForm, _strStationTo, _dtDateFrom, _dtDateTo, _iAdut, _iChild, _iBaby));

            wkBus.Run();

            Random r = new Random();
            List<ComparePriceModel> lst = new List<ComparePriceModel>();
            actual = actual.Where(m => m.TypeWay == type).ToList();
            actual1 = actual1.Where(m => m.TypeWay == type).ToList();
            actual2 = actual2.Where(m => m.TypeWay == type).ToList();
            for (int i = 0; i < actual.Count; i++)
            {
                ComparePriceModel s = new ComparePriceModel();
                s.RepName = "Name: " + i.ToString();
                s.DateString = actual[i].DateStart.ToString("%d");
                s.VietJetPrice = actual[i].Price;
                try
                {
                    s.JetstarPrice = actual1.Where(m => m.DateStart == actual[i].DateStart)
                        .SingleOrDefault().Price;
                }
                catch
                {

                }
                try
                {
                    s.VietNamPrice = actual2.Where(m => m.DateStart == actual[i].DateStart)
                        .SingleOrDefault().Price;
                }
                catch
                {

                }
                lst.Add(s);
            }
            return lst;
        }

        List<TicketDTO> actual = new List<TicketDTO>();
        List<TicketDTO> actual1 = new List<TicketDTO>();
        List<TicketDTO> actual2 = new List<TicketDTO>();

        private void ComineTickets(object data)
        {
            try
            {
                this.actual.AddRange((List<TicketDTO>)data);
            }
            catch (Exception ex)
            {
                LogManager.GetInstance().Error("VietJet: Lỗi tìm vé rẻ trong tháng", ex);
            }
        }
        private void ComineTickets1(object data)
        {
            try
            {
                this.actual1.AddRange((List<TicketDTO>)data);
            }
            catch (Exception ex)
            {
                LogManager.GetInstance().Error("Jetstar: Lỗi tìm vé rẻ trong tháng", ex);
            }
        }
        private void ComineTickets2(object data)
        {
            try
            {
                this.actual2.AddRange((List<TicketDTO>)data);
            }
            catch (Exception ex)
            {
                LogManager.GetInstance().Error("VietNamAir: Lỗi tìm vé rẻ trong tháng", ex);
            }
        }
    }
}
//0908460386